package com.tmpt.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {
	/** 
     * 对图片进行放大 
     * @param originalImage 原始图片 
     * @param times 放大倍数 
     * @return 
     */
	
    public static BufferedImage  zoomInImage(BufferedImage  originalImage, Integer times){ 
        int width = originalImage.getWidth()*times; 
        int height = originalImage.getHeight()*times; 
        BufferedImage newImage = new BufferedImage(width,height,originalImage.getType()); 
        Graphics g = newImage.getGraphics(); 
        g.drawImage(originalImage, 0,0,width,height,null); 
        g.dispose(); 
        return newImage; 
    } 
    /** 
     * 对图片进行放大 
     * @param srcPath 原始图片路径(绝对路径) 
     * @param newPath 放大后图片路径（绝对路径） 
     * @param times 放大倍数 
     * @return 是否放大成功 
     */
    public static boolean zoomInImage(String srcPath,String newPath,Integer times){ 
        BufferedImage bufferedImage = null; 
        try { 
            File of = new File(srcPath); 
            if(of.canRead()){ 
                bufferedImage =  ImageIO.read(of); 
            } 
        } catch (IOException e) { 
            //TODO: 打印日志 
            return false; 
        } 
        if(bufferedImage != null){ 
            bufferedImage = zoomInImage(bufferedImage,times); 
            try { 
                //TODO: 这个保存路径需要配置下子好一点 
                ImageIO.write(bufferedImage, "JPG", new File(newPath)); //保存修改后的图像,全部保存为JPG格式 
            } catch (IOException e) { 
                // TODO 打印错误信息 
                return false; 
            } 
        } 
        return true; 
    } 
    /** 
     * 对图片进行缩小 
     * @param originalImage 原始图片 
     * @param times 缩小倍数 
     * @return 缩小后的Image 
     */
    public static BufferedImage  zoomOutImage(BufferedImage  originalImage, Integer times){ 
        int width = originalImage.getWidth()/times; 
        int height = originalImage.getHeight()/times; 
        BufferedImage newImage = new BufferedImage(width,height,originalImage.getType()); 
        Graphics g = newImage.getGraphics(); 
        g.drawImage(originalImage, 0,0,width,height,null); 
        g.dispose(); 
        return newImage; 
    } 
    /** 
     * 对图片进行缩小 
     * @param srcPath 源图片路径（绝对路径） 
     * @param newPath 新图片路径（绝对路径） 
     * @param times 缩小倍数 
     * @return 保存是否成功 
     */
    public static boolean zoomOutImage(String srcPath,String newPath,Integer times){ 
        BufferedImage bufferedImage = null; 
        try { 
            File of = new File(srcPath); 
            if(of.canRead()){ 
                bufferedImage =  ImageIO.read(of); 
                //.out.println(bufferedImage);
            } 
        } catch (IOException e) { 
            //TODO: 打印日志 
            return false; 
        } 
        //.out.println(bufferedImage);
        if(bufferedImage != null){ 
            bufferedImage = zoomOutImage(bufferedImage,times); 
            try { 
                //TODO: 这个保存路径需要配置下子好一点 
            	//.out.println("保存后的图像");
                ImageIO.write(bufferedImage, "JPG", new File(newPath)); //保存修改后的图像,全部保存为JPG格式 
            } catch (IOException e) { 
                // TODO 打印错误信息 
                return false; 
            } 
        } 
        return true; 
    } 
    
    public static boolean customImage(String srcPath, String newPath,  int width, int height){
        File file = new File(srcPath); 
        return ImageUtils.customImage(file, newPath, width, height);
    }

    public static boolean customImage(File file, String newPath,  int width, int height){
    	BufferedImage bufferedImage = null; 
        try { 
            if(file.canRead()){ 
                bufferedImage =  ImageIO.read(file); 
            } 
        } catch (IOException e) { 
            return false; 
        } 
    	Image scaledImage=bufferedImage.getScaledInstance(width,height, Image.SCALE_SMOOTH);
    	
    	savePic(scaledImage, newPath);
    	
    	return true;
    }
    public static void savePic(Image image ,String newPath){ 
        int w = image.getWidth(null); 
        int h = image.getHeight(null);

        //首先创建一个BufferedImage变量，因为ImageIO写图片用到了BufferedImage变量。 
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);

        //再创建一个Graphics变量，用来画出来要保持的图片，及上面传递过来的Image变量 
        Graphics g = bi.getGraphics(); 
        try { 
            g.drawImage(image, 0, 0, null);
            //将BufferedImage变量写入文件中。 
            ImageIO.write(bi,"jpg",new File(newPath)); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    }

    
    public static void main(String[] args) { 
    	boolean b = ImageUtils.customImage("c:/1.jpg","c:/1.4.4.jpg", 400, 400);
    	//.out.println(b);
        //boolean testOut = zoomOutImage("c:/1.jpg","c:/1_small.jpg",4); 
        
        /*
        if(testOut){ 
            //.out.println("out ok");
        } 
        */
    } 
  
} 
